package com.formation.userbase.webapp.actions;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.formation.userbase.persistance.beans.User;
import com.formation.userbase.persistance.services.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport implements SessionAware {
	Map session;
	String login;
	String password;
	User u;
	List<User> lUser;


	UserService userv ;
	
	
	public UserService getUserv() {
		return userv;
	}

	public void setUserv(UserService userv) {
		this.userv = userv;
	}

	//action appele dans la page de login
	public String UserVerifLogin(){
		u= userv.findUserByLogin(this.getLogin());
		
		if(u!=null){
		  if(u.getMdp().equals(this.getPassword())){
			  session.put("login", u.getLogin());
			  return SUCCESS;
		  }
			
		}
		return ERROR;
		
	}
	
	public String UserAfficheList(){
		lUser= userv.getAllUser();
		
		return SUCCESS;
	}
	
	
	public String AfficheForminscription(){
		
		return SUCCESS;
	}
	
	public String UserverifInscription(){
		if(userv.insert(getU())){
			addActionMessage("inscription realise avec succes, vous pouvez desormais vous logguer");
			return "success";
		}
		else{
			addActionError("erreur inscription verifiez vos champs");
			return "error";
		}
	}
	
	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public User getU() {
		return u;
	}

	public void setU(User u) {
		this.u = u;
	}

	public Map getSession() {
		return session;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}

	public List<User> getLUser() {
		return lUser;
	}

	public void setLUser(List<User> user) {
		lUser = user;
	}
	
}
